package com.canbot.u05.adapter.dance

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.TextView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.canbot.u05.R
import com.canbot.u05.activity.logd
import com.canbot.u05.bean.dance.DanceListItemBean
import com.canbot.u05.bean.dance.ResponseInfo
import com.canbot.u05.glide.GlideApp
import com.canbot.u05.network.DanceUtil
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.widget.RoundImageView
import kotlinx.android.synthetic.main.dance_manager_item.view.*

class DanceAdapter(internal val mContext: Context, private val mbrowList: List<DanceListItemBean>) : RecyclerView.Adapter<DanceAdapter.MyViewHolder>() {

        private var index: Int = 0
        lateinit var function: (Int) -> Unit
        inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {

                var iconName: TextView = view.text
                var iconImageView: RoundImageView = view.image
                var use: CheckBox = view.use
                var nouse: CheckBox = view.nouse
                var delDance:Button = view.del_dance

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                val view = LayoutInflater.from(mContext).inflate(R.layout.dance_manager_item, parent, false)
                return MyViewHolder(view)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                val (id, name, imageUrl, enabled,type, function) = mbrowList[position]
                holder.iconName.text = name
                logd("imageUrl:" + imageUrl)
                GlideApp.with(mContext).clear(holder.iconImageView)
                GlideApp.with(mContext)
                        .asDrawable()
                        .load(IpMsgConst.HTTP_URL_CHEST + imageUrl)
                        .thumbnail(0.1f)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .skipMemoryCache(false)
                        .placeholder(R.drawable.sales_banner01)//占位图
                        .error(R.drawable.ic_picture_loadfailed)
                        //.override(100, 100)
                        .into(holder.iconImageView)

                if (enabled) {
                        holder.use.isChecked = true
                        holder.nouse.isChecked = false
                } else {
                        holder.use.isChecked = false
                        holder.nouse.isChecked = true
                }
                holder.use.setOnCheckedChangeListener { buttonView, isChecked ->
                        run {
                                if (isChecked) {
                                        holder.use.isChecked = true
                                        holder.nouse.isChecked = false
                                        editEnabled(id, true)

                                } else {
                                        holder.use.isChecked = false
                                        holder.nouse.isChecked = true
                                }
                        }
                }

                holder.nouse.setOnCheckedChangeListener { buttonView, isChecked ->
                        run {
                                if (isChecked) {
                                        holder.use.isChecked = false
                                        holder.nouse.isChecked = true
                                        editEnabled(id, false)
                                } else {
                                        holder.use.isChecked = true
                                        holder.nouse.isChecked = false
                                }
                        }

                }
                //长按显示删除按钮
                holder.iconImageView.setOnLongClickListener {
                        if (type.equals("custom")){
                                holder.delDance.visibility = View.VISIBLE
                        }
                        false
                }

                holder.iconImageView.setOnClickListener{
                        function()
                }

                holder.delDance.setOnClickListener {
                        if (type.equals("custom")){
                                logd("删除条目"+position)
                                function(position)
                        }
                }
        }
        fun setDeleteListener(function:(id:Int)->Unit){
                this.function = function
        }
        override fun getItemId(i: Int): Long {
                return i.toLong()
        }

        override fun getItemCount(): Int {
                return mbrowList.size
        }

        fun setIndex(i: Int) {
                index = i
        }


        private fun editEnabled(id: Int, enabled: Boolean) {
                DanceUtil.editEnabled(id, enabled, object : DanceUtil.DanceCallBack<ResponseInfo> {
                        override fun onSuccess(result: ResponseInfo) {
                                if (result.error_code == 0) {

                                }
                        }

                        override fun onFailure() {

                        }
                })

        }


}
